<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.dicLabel"
          placeholder="请输入字典名称"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.dicStatus"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加字典</span
        >
        <span class="wrap-btn wrap-go-back" @click="go">返回</span>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="dicLabel" label="字典名称"> </el-table-column>
        <el-table-column prop="dicValue" label="字典值"> </el-table-column>
        <el-table-column prop="dicDesc" label="字典描述">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.dicDesc"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.dicDesc }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.dicStatus == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.dicStatus == 1"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item v-if="$checkPermission(101)" command="add"
                  >新增</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.dicStatus == 1 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.dicStatus == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="120px" :model="form" size="medium">
        <el-form-item label="字典名称" required>
          <el-input
            v-model.trim="form.dicLabel"
            clearable
            maxlength="20"
            placeholder="请输入字典名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="字典值" required>
          <el-input
            v-model.trim="form.dicValue"
            clearable
            maxlength="20"
            placeholder="请输入字典值"
          ></el-input>
        </el-form-item>
        <el-form-item label="字典描述">
          <el-input
            type="textarea"
            v-model.trim="form.dicDesc"
            placeholder="请输入字典描述"
            maxlength="100"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="字典名称">
          {{ lockData.dicLabel }}
        </el-descriptions-item>
        <el-descriptions-item label="字典值">
          {{ lockData.dicValue }}
        </el-descriptions-item>
        <el-descriptions-item label="字典描述">
          {{ lockData.dicDesc }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {
          dicType: this.$route.query.type,
        },
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加字典',
      form: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/dic/pageDic', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    add() {
      //添加
      this.form = {
        id: 0,
        dicStatus: '0',
        parentCode: 0,
        dicType: this.$route.query.type,
      }
      this.title = '添加字典'
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.lockData = JSON.parse(JSON.stringify(row))
          this.isLook = true
          break
        case 'add':
          this.form = {
            id: 0,
            dicStatus: '0',
            parentCode: row.dicValue,
            dicType: this.$route.query.type,
          }
          this.title = '添加字典'
          this.visible = true
          break
        case 'edit':
          this.title = '编辑字典'
          this.form = JSON.parse(JSON.stringify(row))
          this.visible = true
          break
        case 'changeStatus':
          //启用停用
          const status = row.dicStatus == 0 ? 1 : 0
          this.$axios
            .get(
              '/admin/dic/updateStatusDic?id=' + row.id + '&status=' + status
            )
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios.get('/admin/dic/delDic?id=' + row.id).then((data) => {
                if (data.code == 200) {
                  this.itemList()
                  this.$message.success('操作成功！')
                } else {
                  this.$message.error(data.msg)
                }
              })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.dicLabel) {
        this.$message.error('请输入字典名称')
        return
      }
      if (!this.form.dicValue) {
        this.$message.error('请输入字典值')
        return
      }
      this.$axios.post('/admin/dic/editDic', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    go() {
      this.$store.dispatch('tags/delVisitedRoute', {
        path: this.$route.path,
      })
      this.$router.push('/userCenter/dictionaries')
    },
  },
}
</script>
